package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.api.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.service.GoodsService;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.merchant.table.entity.Table;
import com.hlf.modules.merchant.table.entity.TableType;
import com.hlf.modules.merchant.table.service.TableService;
import com.hlf.modules.merchant.table.service.TableTypeService;
import com.hlf.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/29.
 * 餐桌管理接口
 */
@RestController
@RequestMapping("/api/table")
@Api("餐桌管理接口")
public class ApiTableController {

    @Autowired
    private TableTypeService tableTypeService;
    @Autowired
    private TableService tableService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StoreService storeService;

    /**
     * 创建餐桌
     */
    @SysLog("创建餐桌")
    @PostMapping("create")
    @ApiOperation(value = "创建餐桌", notes = "创建餐桌")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeId", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableNum", value = "餐桌编号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult createTable(String tableTypeId, String tableNum, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableTypeId, "餐桌类型id不能为空");
        Assert.isBlank(tableNum, "餐桌编号不能为空");

        Long userId = user.getUserId();
        String name = tableTypeService.queryObjectForId(Long.parseLong(tableTypeId)).getName();

        Table table = new Table();
        table.setId(IdWorkerFactory.nextId());
        table.setTableName(name);
        table.setTableType(Long.parseLong(tableTypeId));
        table.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        table.setTableNum(tableNum.toUpperCase());
        table.setInsertBy(userId);

        int i = tableService.insertSelective(table);
        if (i == 0) {
            return ReturnResult.error("创建失败!");
        }

        return ReturnResult.ok();
    }

    /**
     * 修改餐桌
     */
    @SysLog("修改餐桌")
    @PostMapping("update")
    @ApiOperation(value = "修改餐桌", notes = "修改餐桌")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeId", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableNum", value = "餐桌编号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateTable(String tableId, String tableTypeId, String tableNum, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableTypeId, "餐桌类型id不能为空");
        Assert.isBlank(tableId, "餐桌id不能为空");
        Assert.isBlank(tableNum, "餐桌编号不能为空");

        Long userId = user.getUserId();
        String name = tableTypeService.queryObjectForId(Long.parseLong(tableTypeId)).getName();

        Table table = new Table();
        table.setId(Long.parseLong(tableId));
        table.setTableName(name);
        table.setTableType(Long.parseLong(tableTypeId));
        table.setTableNum(tableNum.toUpperCase());
        table.setLastUpdateBy(userId);

        int i = tableService.updateByPrimaryKeySelective(table);
        if (i == 0) {
            return ReturnResult.error("修改失败!");
        }

        return ReturnResult.ok();
    }


    /**
     * 删除餐桌/批量
     */
    @SysLog("批量删除餐桌")
    @PostMapping("delTable")
    @ApiOperation(value = "删除餐桌", notes = "删除餐桌")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableIds", value = "餐桌id/可多个", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult deleteTable(HttpServletRequest request, @RequestParam(value = "tableIds", required = true) List<String> tableIds) {
        Assert.isNull(tableIds, "餐桌id不能为空");

        tableService.deleteTable(request, tableIds);
        return ReturnResult.ok();
    }

    /**
     * 餐桌列表
     */
    @GetMapping("listTable")
    @ApiOperation(value = "餐桌列表", notes = "餐桌列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult tableList(@LoginUser SysUserEntity user) {

        List<TableType> tables = tableTypeService.queryDepthListForStoreId(Long.parseLong(user.getCurrentStoreId()));
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, tables);
        return ReturnResult.ok(map);
    }

    /**
     * 堂食点餐 返回餐桌列表
     */
    @GetMapping("listTableForOrder")
    @ApiOperation(value = "堂食点餐 返回餐桌列表", notes = "堂食点餐 返回餐桌列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult tableListForOrder(@LoginUser SysUserEntity user) {

        List<TableType> tables = tableTypeService.queryDepthListForStoreId(Long.parseLong(user.getCurrentStoreId()));
        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, tables);
        return ReturnResult.ok(map);
    }

    /**
     * 点餐选择餐桌
     */
    @GetMapping("useTable/{tableId}")
    @ApiOperation(value = "点餐选择餐桌", notes = "点餐选择餐桌")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "tableId", value = "餐桌id", required = true)
    })
    public ReturnResult useTable(@PathVariable("tableId") String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");
        //修改餐桌状态
        Table table = tableService.updateTableStatus(Long.parseLong(tableId));
        //返回茶位费
        Goods goods = goodsService.queryTeaForStoreId(table.getStoreId());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, goods);
    }

    /**************************************************** 餐桌类型 ***************************************************/
    /**
     * 创建餐桌类型
     */
    @SysLog("创建餐桌类型")
    @PostMapping("createType")
    @ApiOperation(value = "创建餐桌类型", notes = "创建餐桌类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeName", value = "餐桌类型名称", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "minNumber", value = "允许最少就餐人数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "maxNumber", value = "允许最大就餐人数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult createTableType(String tableTypeName, String maxNumber, String minNumber, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableTypeName, "餐桌类型名称不能为空");
        Assert.isBlank(minNumber, "允许最少就餐人数不能为空");
        Assert.isBlank(maxNumber, "允许最大就餐人数不能为空");

        Long userId = user.getUserId();
        Integer minInt = Integer.valueOf(minNumber);
        Integer maxInt = Integer.parseInt(maxNumber);
        if (maxInt > 255) {
            return ReturnResult.error("最多可选择255!");
        }
        TableType tableType = new TableType();
        tableType.setId(IdWorkerFactory.nextId());
        tableType.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        tableType.setName(tableTypeName);
        tableType.setMinNum(minInt);
        tableType.setMaxNum(maxInt);
        tableType.setInsertBy(userId);

        int i = tableTypeService.insertSelective(tableType);
        if (i == 0) {
            return ReturnResult.error("添加失败!");
        }

        return ReturnResult.ok();
    }

    /**
     * 餐桌类型列表
     */
    @GetMapping("listType")
    @ApiOperation(value = "餐桌类型列表", notes = "餐桌类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult tableTypeList(@LoginUser SysUserEntity user) {

        List<TableType> tableTypes = tableTypeService.queryListForStoreId(Long.parseLong(user.getCurrentStoreId()));

        Map<String, Object> map = new HashMap<>();
        map.put(Constants.DEFAULT_RESULT_NAME, tableTypes);
        return ReturnResult.ok(map);
    }

    /**
     * 修改餐桌类型
     */
    @SysLog("修改餐桌类型")
    @PostMapping("updateType")
    @ApiOperation(value = "修改餐桌类型", notes = "修改餐桌类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableName", value = "餐桌名称", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "minNumber", value = "允许最少就餐人数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "maxNumber", value = "允许最大就餐人数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "id", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult updateTableType(HttpServletRequest request, String tableName, String maxNumber, String minNumber, String id) {
        Assert.isBlank(tableName, "餐桌名称不能为空");
        Assert.isBlank(minNumber, "允许最少就餐人数不能为空");
        Assert.isBlank(maxNumber, "允许最大就餐人数不能为空");
        Assert.isBlank(id, "餐桌类型id不能为空");

        Integer minInt = Integer.valueOf(minNumber);
        Integer maxInt = Integer.parseInt(maxNumber);
        if (maxInt > 255) {
            return ReturnResult.error("最多可选择255!");
        }
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        TableType tableType = new TableType();
        tableType.setMaxNum(maxInt);
        tableType.setMinNum(minInt);
        tableType.setName(tableName);
        tableType.setLastUpdateBy(userId);
        tableType.setId(Long.parseLong(id));
        tableTypeService.updateByPrimaryKeySelective(tableType);

        return ReturnResult.ok();
    }

    /**
     * 删除餐桌类型
     */
    @SysLog("修改餐桌类型")
    @PostMapping("delType")
    @ApiOperation(value = "删除餐桌类型", notes = "删除餐桌类型")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "id", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult deleteTableType(HttpServletRequest request, String id) {
        Assert.isBlank(id, "餐桌类型id不能为空");

        tableTypeService.deleteTableType(request, id);
        return ReturnResult.ok();
    }
/**************************************************** 餐桌二维码 ***************************************************/

    /**
     * 邮件发送二维码
     */
    @SysLog("邮件发送二维码")
    @PostMapping("sendMail")
    @ApiOperation(value = "邮件发送二维码", notes = "邮件发送二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "email", value = "邮件地址", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableIdList", value = "餐桌id/可多个", required = true),
    })
    public ReturnResult sendMail(String email, @RequestParam(value = "tableIdList", required = true) List<Long> tableIdList) {
        Assert.isBlank(email, "邮件地址不能为空");
        Assert.isNull(tableIdList, "餐桌id不能为空");
        for (Long tableId : tableIdList) {
            Table table = tableService.queryObjectForId(tableId);
            Store store = storeService.queryObject(table.getStoreId());
            tableService.sendEmail(email,store.getName());
        }

        return ReturnResult.ok();
    }

    /**
     * 保存二维码到手机/预览
     */
    @SysLog("手机保存二维码")
    @PostMapping("saveImg")
    @ApiOperation(value = "保存二维码到手机/预览", notes = "保存二维码到手机/预览")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableIdList", value = "餐桌id/可多个", required = false)
    })
    public ReturnResult saveImg(@RequestParam(value = "tableIdList", required = false) List<Long> tableIdList) {
        String url = "http://192.168.0.10:83/storeImg/2017/10/25/f38b513dd39f4dae940951562a87f32f.png";
        //示例图
        if (tableIdList == null || tableIdList.size() == 0) {
            return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, url);
        }
        List<String> list = new ArrayList<>();
        list.add(url);
        list.add(url);
        list.add(url);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,list);
    }


}
